package com.liang.gulimall.order.interceptor;

import com.liang.common.constant.AuthServerConstant;
import com.liang.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberVo> loginUserThread=new ThreadLocal<>();
    /**
     * 目标方法前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute(AuthServerConstant.LOGIN_USER);
        loginUserThread.set((MemberVo) loginUser);
        ///order/order/orderInfo/{orderSn}
        String uri = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();

        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match = antPathMatcher.match("/order/**", uri);
        boolean match1 = antPathMatcher.match("/order/payed/notify", uri);
        System.out.println(uri+"-------------");
        if(match || match1){
            return true;
        }
        if (loginUser==null){

            session.setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }

        return true;
    }
}
